// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'cloud1-1ggcv3455c399089',
  traceUser: true
})

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  switch (event.action) {
    case 'getUrlScheme': {
      return getUrlScheme(event.options)
    }
  }

  return 'action not found'
}

async function getUrlScheme(options) {
  const param = {
    jumpWxa: {
      path: options.path, 
      query: options.query,
      envVersion: options.env || 'release'
    },
    // 如果想不过期则置为 false，并可以存到数据库
    isExpire: options.isExpire || false,
    // 24小时有效期
    expireTime:options.expireTime ||  parseInt(Date.now() / 1000 + 60*60*24)
  }

  console.log('urlscheme.generate的参数:', param)

  return cloud.openapi.urlscheme.generate(param)
}